package com.singzi.leetcode.N100;


public class T0086 {
    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(4);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(2);
        head.next.next.next.next = new ListNode(5);
        head.next.next.next.next.next = new ListNode(2);
        head.next.next.next.next.next.next = new ListNode(22);
        head.next.next.next.next.next.next.next = new ListNode(2);
        Solution0086 solution0086 = new Solution0086();
        System.out.println(solution0086.partition(head, 3));
        ListNode t = solution0086.partition(head, 3);
        while (t!=null){
            System.out.println(t.val);
            t = t.next;
        }


    }

}

class ListNode {
   int val;
   ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 }

class Solution0086 {
    public ListNode partition(ListNode head, int x) {

        ListNode front = new ListNode();
        ListNode rear = new ListNode();
        ListNode f = front;
        ListNode r = rear;
        while (head!=null){
            if(head.val < x){
                front.next = new ListNode(head.val);
                front = front.next;
            }
            else {
                rear.next = new ListNode(head.val);
                rear = rear.next;
            }
            head = head.next;
        }
        front.next = r.next;

        return f.next;

    }
}